package com.auto.learning.fragment;

import java.sql.SQLException;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.auto.learning.MainActivity;
import com.auto.learning.R;
import com.auto.learning.activity.CommentActivity;
import com.auto.learning.activity.PlayActivity;
import com.auto.learning.activity.ReadBookActivity;
import com.auto.learning.bean.BookBean;
import com.auto.learning.net.HttpCommonInfo;
import com.auto.learning.sharesdk.tools.ShareOpUtils;
import com.auto.learning.store.core.StaticReference;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.TemporaryVariable;
import com.auto.learning.view.ShareView;

public class ReadFinishFragment extends Fragment implements OnClickListener,
		Callback {

	private LinearLayout linear_comment, linear_goto_home,
			linear_read_nextbook;
	private ImageView image_share;
	private RelativeLayout relative_main;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.readfinish_fragment, null);
		initView(view);
		return view;
	}

	private void initView(View view) {
		linear_comment = (LinearLayout) view.findViewById(R.id.linear_comment);
		linear_comment.setOnClickListener(this);
		linear_goto_home = (LinearLayout) view
				.findViewById(R.id.linear_goto_home);
		linear_goto_home.setOnClickListener(this);
		linear_read_nextbook = (LinearLayout) view
				.findViewById(R.id.linear_read_nextbook);
		linear_read_nextbook.setOnClickListener(this);
		image_share = (ImageView) view.findViewById(R.id.image_share);
		image_share.setOnClickListener(this);
		relative_main = (RelativeLayout) view.findViewById(R.id.relative_main);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.linear_comment:
			/*
			 * ReadBookActivity readBookActivity = (ReadBookActivity)
			 * getActivity(); Intent intent = new Intent(getActivity(),
			 * CommentActivity.class); intent.putExtra("bookId",
			 * readBookActivity.getBookId()); intent.putExtra("bookName",
			 * readBookActivity.getBookName()+""); intent.putExtra("imgFace",
			 * readBookActivity.getFaceImg()); intent.putExtra("imgFacePlay",
			 * readBookActivity.getFaceImgPlay()); startActivity(intent);
			 */

			ReadBookActivity readBookActivity = (ReadBookActivity) getActivity();
			if (TemporaryVariable.playActivity != null
					&& TemporaryVariable.playActivity instanceof PlayActivity) {
				TemporaryVariable.playActivity.finish();
			}
			Intent intent = new Intent(getActivity(), PlayActivity.class);
			intent.putExtra("bookId", readBookActivity.getBookId());
			intent.putExtra("faceImg", readBookActivity.getFaceImg());
			BookBean bean=null;
			try {
				bean = StaticReference.mutilDBSupport.getDao(
						BookBean.class).queryForId(readBookActivity.getBookId());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (bean != null) {
				intent.putExtra("canRead", bean.getCanRead());
				intent.putExtra("needBuy", bean.getNeedBuy());
			}

			intent.putExtra("type", 2);
			intent.putExtra("isComment", true);
			startActivity(intent);
			readBookActivity.finish();
			break;
		case R.id.linear_goto_home:
			TemporaryVariable.gotoHome();
			getActivity().startActivity(
					new Intent(getActivity(), MainActivity.class));
			getActivity().finish();
			break;
		case R.id.linear_read_nextbook:
			ReadBookActivity nextBook = (ReadBookActivity) getActivity();
			int position = TemporaryVariable.read_list.indexOf(nextBook
					.getBookId());
			if (TemporaryVariable.read_list.size() > position + 1) {
				
				nextBook.refreshUIData(TemporaryVariable.read_list.get(position + 1));
			} else {
				CommonMethod.makeNotice(getActivity(), "已经是最后一本书啦！");
			}
			break;
		case R.id.image_share:
			ReadBookActivity shareBean = (ReadBookActivity) getActivity();
			new ShareView(getActivity(), this)
					.setTitle(shareBean.getBookName())
					.setUrl(HttpCommonInfo.MURL + "authorware-detail?id="
							+ shareBean.getBookId())
					.setContent(shareBean.getSlogan())
					.setImageUrl(shareBean.getFaceImgPlay())
					.setId(shareBean.getBookId() + "").show(relative_main);
			break;
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		ShareOpUtils.handleShareCallBack(getActivity(), msg);
		return false;
	}
}
